/**
 * Copyright: Copyright (c) 2012
 * Company:深圳市海乐淘电子商务有限公司
 * @author lifuyu
 * @date 2012-5-22 下午10:06:58
 * @version V1.0
 *
 * @Description: 系统全局的业务DAO
 */
package com.microcm.system.dao;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.stereotype.Repository;

import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.microcm.manage.entity.Goods;
import com.microcm.manage.entity.SyncShop;
import com.microcm.member.entity.User;
import com.microcm.system.entity.SysArea;
import com.microcmframework.core.dao.SqlMapDao;

@Repository("systemDao")
public class SystemDao extends SqlMapDao
{
	public SystemDao()
	{
		super(SystemDao.class.getName());
	}

	/**
	 * 调用generateSerialNo存储过程生成系统唯一序列号
	 * 
	 * @param stype
	 *            系列类型
	 * @param staff
	 *            系列前缀
	 * @param len
	 *            长度
	 * @return
	 */
	public String generateSerialNo(String stype, String staff, int len)
	{
		try
		{
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("i_stype", stype);
			paramMap.put("i_staff", staff);
			paramMap.put("i_len", len);

			this.getSqlMapClient().update("system.generateSerialNo", paramMap);
			return String.valueOf(paramMap.get("o_serialNo"));
		}
		catch (Exception e)
		{
			logger.error("生成系统系列号异常", e);
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 批量更新淘宝的省市区基础数据
	 * 
	 * @author：lifuyu
	 * @param addressLs
	 * @return
	 */
	public boolean batchAreasSave(final List<SysArea> areaList)
	{
		boolean ret = (Boolean) getSqlMapClientTemplate().execute(new SqlMapClientCallback<Object>()
		{
			public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException
			{
				executor.startBatch();
				for (SysArea area : areaList)
				{
					Object o = executor.queryForObject("system.isAreaExist", area.getId());
					int c = (Integer) o;
					if (c == 0)
					{
						executor.insert("system.insertArea", area);
					}
				}
				int uc = executor.executeBatch();
				return uc > 0;
			}
		});
		return ret;
	}

	/**
	 * 批量更新商品的索引标记，置为已经被索引了
	 * 
	 * @author：lifuyu
	 * @param addressLs
	 * @return
	 */
	public boolean batchUpdateGoodsHasIndex(final List<?> goodsList)
	{
		boolean ret = (Boolean) getSqlMapClientTemplate().execute(new SqlMapClientCallback<Object>()
		{
			public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException
			{
				executor.startBatch();
				for (Object o : goodsList)
				{
					Goods goods = (Goods) o;
					executor.update("system.updateGoodsHasIndex", goods.getNumIid());
				}
				int uc = executor.executeBatch();
				return uc > 0;
			}
		});
		return ret;
	}

	/**
	 * 批量删除商品，被删除的商品是已经清理过索引的商品
	 * 
	 * @author：lifuyu
	 * @param addressLs
	 * @return
	 */
	public boolean batchDeleteGoodsHasCleanIndex(final List<?> goodsList)
	{
		boolean ret = (Boolean) getSqlMapClientTemplate().execute(new SqlMapClientCallback<Object>()
		{
			public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException
			{
				executor.startBatch();
				for (Object o : goodsList)
				{
					Goods goods = (Goods) o;
					executor.delete("system.deleteGoodsHasCleanIndex", goods.getNumIid());
				}
				int uc = executor.executeBatch();
				return uc > 0;
			}
		});
		return ret;
	}

	/**
	 * 批量更新店铺的索引标记，置为已经被索引了
	 * 
	 * @author：lifuyu
	 * @param addressLs
	 * @return
	 */
	public boolean batchUpdateShopsHasIndex(final List<?> shopsList)
	{
		boolean ret = (Boolean) getSqlMapClientTemplate().execute(new SqlMapClientCallback<Object>()
		{
			public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException
			{
				executor.startBatch();
				for (Object o : shopsList)
				{
					SyncShop shop = (SyncShop) o;
					executor.update("system.updateShopsHasIndex", shop.getSid());
				}
				int uc = executor.executeBatch();
				return uc > 0;
			}
		});
		return ret;
	}
	
	/**
	 * 批量更新用户的索引标记，置为已经被索引了
	 * 
	 * @author：lifuyu
	 * @param addressLs
	 * @return
	 */
	public boolean batchUpdateUsersHasIndex(final List<?> usersList)
	{
		boolean ret = (Boolean) getSqlMapClientTemplate().execute(new SqlMapClientCallback<Object>()
		{
			public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException
			{
				executor.startBatch();
				for (Object o : usersList)
				{
					User user = (User) o;
					executor.update("system.updateUserHasIndex", user.getUnid());
				}
				int uc = executor.executeBatch();
				return uc > 0;
			}
		});
		return ret;
	}
}